# Entity manager
from pyston import *

class EntityManager:
    
    # Constructor
    def __init__(self, rsMan):
        self.currentEntities = {}
        self.resourceManager = rsMan
    
    # Automatically runs framestep on registered entities    
    def stepFrame(self, timeSinceLastFrame):
        for cName, cEnt in self.currentEntities.iteritems():
            cEnt.stepFrame(timeSinceLastFrame)
            
    def draw(self, parentSurface):
        for cName, cEnt in self.currentEntities.iteritems():
            if cEnt.getDisplayMode() != dm_none:
                cEnt.draw(parentSurface)
    
    # Add Entity
    # - Adds a named entity
    # - Sets its name
    # - Calls its activate() method        
    def addEntity(self, name, newEntity):
        self.currentEntities[name] = newEntity
        self.currentEntities[name].setName(name)
        self.currentEntities[name].setResourceManager(self.resourceManager)
        self.currentEntities[name].activate()
        
    def addEntities(self, prefix, newEntities):
        # Loop through the items and add the prefix (1 based)
        prefixValue = 1
        
        for e in newEntities:
            self.addEntity(prefix + "_%d" % prefixValue, e)
            prefixValue += 1
        
    def removeEntity(self, name):
        if self.currentEntities.has_key(name):
            self.currentEntities[name].deactivate()
            del self.currentEntities[name]
        else:
            debug_out("EntityManager - Entity: " + name + " does not exist (removeEntity)")
            